package leetCode.offer28;

public class Solution1 implements Solution{



    @Override
    public boolean isSymmetric(TreeNode root) {
        if(root==null) return true;
        TreeNode mirror = new TreeNode(root.val);
        rightToLeft(root,mirror);
        return deepIterator(root,mirror);
    }


    /**
     * 计算一个树的镜像
     * @param root
     * @param target
     */
    public static void rightToLeft(TreeNode root,TreeNode target){
        if(root==null){
            return;
        }
        if(root.right!=null){
            target.left = new TreeNode(root.right.val);
        }
        if(root.left!=null){
            target.right = new TreeNode(root.left.val);
        }
        rightToLeft(root.right,target.left);
        rightToLeft(root.left,target.right);
    }


    /**
     * 比较两颗树是否相等
     * @param root1
     * @param root2
     * @return
     */
    public static boolean deepIterator(TreeNode root1,TreeNode root2) {
        if(root1 == null) {
            return root2 == null;
        }
        if(root2==null){
            return false;
        }
        if(root1.val!=root2.val){
            return false;
        }
        boolean left = deepIterator(root1.left,root2.left);
        boolean right = deepIterator(root1.right,root2.right);
        return left&&right;
    }

    public static boolean deepIterator2(TreeNode root1,TreeNode root2) {
        if(root1 == null) {
            return true;
        }
        if(root2 == null){
            return false;
        }
        if(root1.val!=root2.val){
            return false;
        }
        boolean left = deepIterator2(root1.left,root2.left);
        boolean right = deepIterator2(root1.right,root2.right);
        return left&&right;
    }

}
